package com.xtxk.utils.util;

import java.io.File;
import java.util.Properties;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

import com.xtxk.encrypt.EncryptLib;

public class ConfigHelper
{
	// 不被修改的静态变量
	private static final String LOCAL_IP = "localip";
	private static final String LOCAL_PORT = "localport";
	private static final String DB_TYPE = "dbtype";
	private static final String DB_IP = "dbip";
	private static final String DB_PORT = "dbport";
	private static final String DB_SOURCE = "dbsource";
	private static final String DB_UID = "dbuid";
	private static final String DB_PWD = "dbpwd";
	private static final String POOL_ING = "pooling";
	private static final String MIN_POOLSIZE = "minpoolsize";
	private static final String MAX_POOLSIZE = "maxpoolsize";
	private static final String CACHE_SWITCH = "cacheswitch";
	private static final String CACHE_TYPE = "cachetype";
	private static final String CACHE_IP = "cacheip";
	private static final String CACHE_PORT = "cacheport";
	private static final String CACHE_EXPIRE = "cacheexpire";
	private static final String SYSCHRO_SWITCH = "synchroswitch";
	private static final String SYSCHRO_PORT = "synchroport";
	private static final String GATWAY_IP = "gatewayip";
	private static final String GATEWAY_PORT = "gatewayport";
	
	private static final String LOG_TYPE = "logtype";
	private static final String LOG_LEVEL = "loglevel";
	private static final String REMOTELOG_LEVEL = "remoteloglevel";
	private static final String LOG_SERVERIP = "logserverip";
	private static final String SERVICE_PORT = "serviceport";
	private static final String INVOKE_PORT = "invokeport";
	
	private static final String EncryptSwitch = "encryptswitch";
	private static final String TokenAppKey ="tokenappkey";
	private static final String DefaultEncode = "defaultencode";
	private static final String TokenSwitch = "tokenswitch";
	private static final String BASE_LENGTH = "baselength";
	
	private String configPath;
	
	public String localIP;
	public String localPort;
	public String dbType;
	public String dbIP;
	public String dbPort;
	public String dbSource;
	public String dbUid;
	public String dbPwd;
	public String pooling;
	public String minpoolsize;
	public String maxpoolsize;
	public String cacheswitch;
	public String cachetype;
	public String cacheip;
	public String cacheport;
	public String cacheexpire;
	public String synchroswitch;
	public String synchroport;
	public String gatewayip;
	public String gatewayport;
	public String baselength;
	
	// xt日志
	public String logtype="0";
	public int loglevel;
	public int remoteloglevel;
	public String logserverip;
	public String serviceport;
	public String invokeport;
	
	public int encryptswitch=0;
	public String tokenappkey="";
	public String defaultencode = "";
	public int tokenswitch=0;
	
	public static ConfigHelper instance = new ConfigHelper();
	
	private ConfigHelper()
	{
		// 获取系统的文件属性
		Properties pro = System.getProperties();
		// 获取操作系统的名称
		String sys = pro.getProperty("os.name");
		if (sys.startsWith("W") || sys.startsWith("w"))
		{
			configPath = "d:\\netmcset\\dsu.xml";
		}
		else
		{
			configPath = "/etc/xtconfig/d/netmcset/dsu.xml";
		}
		try
		{
			// SAXReader是一种XML的一种解析方法
			SAXReader saxReader = new SAXReader();
			// 读取文件中的内容
			Document document = saxReader.read(new File(configPath));
			if (document != null)
			{
				Element root = document.getRootElement();
				// XML获取文件中的节点
				Node dsu = root.selectSingleNode("//dsu");
				// 获取文件节点中属性
				if (dsu != null)
				{
					Node TokenSwitchNode = dsu.selectSingleNode(TokenSwitch);
					if (TokenSwitchNode != null)
					{
						String tokenswitchStr = TokenSwitchNode.getText();
						if(tokenswitchStr.length()>0){
							this.tokenswitch = Integer.parseInt(tokenswitchStr);
						}
						
					}
					Node TokenAppKeyNode = dsu.selectSingleNode(TokenAppKey);
					if (TokenAppKeyNode != null)
					{
						this.tokenappkey = TokenAppKeyNode.getText();
					}
					Node DefaultEncodeNode = dsu.selectSingleNode(DefaultEncode);
					if (DefaultEncodeNode != null)
					{
						this.defaultencode = DefaultEncodeNode.getText();
					}
					
					
					Node nodeLocalIP = dsu.selectSingleNode(LOCAL_IP);
					if (nodeLocalIP != null)
					{
						this.localIP = nodeLocalIP.getText();
					}
					
					Node nodeLocalport = dsu.selectSingleNode(LOCAL_PORT);
					if (nodeLocalport != null)
					{
						this.localPort = nodeLocalport.getText();
					}
					
					Node nodedpType = dsu.selectSingleNode(DB_TYPE);
					if (nodedpType != null)
					{
						this.dbType = nodedpType.getText();
					}
					
					Node nodedbIP = dsu.selectSingleNode(DB_IP);
					if (nodedbIP != null)
					{
						this.dbIP = nodedbIP.getText();
					}
					
					Node nodedbPort = dsu.selectSingleNode(DB_PORT);
					if (nodedbPort != null)
					{
						this.dbPort = nodedbPort.getText();
					}
					Node nodedbSource = dsu.selectSingleNode(DB_SOURCE);
					if (nodedbSource != null)
					{
						this.dbSource = nodedbSource.getText();
					}
					
					Node encryptNode = dsu.selectSingleNode(EncryptSwitch);
					
					if (encryptNode != null)
					{
						this.encryptswitch = Integer.parseInt(encryptNode.getText());
					}
					
					Node nodedbUid = dsu.selectSingleNode(DB_UID);
					String uid = "";
					if (nodedbUid != null)
					{
						uid = nodedbUid.getText();
					}
					
					Node nodedbPwd = dsu.selectSingleNode(DB_PWD);
					String password = "";
					if (nodedbPwd != null)
					{
						password = nodedbPwd.getText();
					}
					
					if (this.encryptswitch == 1)
					{
						this.dbUid = EncryptLib.XTDncrypt(uid, uid.length());
						this.dbPwd = EncryptLib.XTDncrypt(password, password.length());
					}
					else
					{
						this.dbUid = uid;
						this.dbPwd = password;
					}
					
					Node nodepooling = dsu.selectSingleNode(POOL_ING);
					
					if (nodepooling != null)
					{
						this.pooling = nodepooling.getText();
					}
					Node nodeminpoolsize = dsu.selectSingleNode(MIN_POOLSIZE);
					
					if (nodeminpoolsize != null)
					{
						this.minpoolsize = nodeminpoolsize.getText();
					}
					Node nodemaxpoolsize = dsu.selectSingleNode(MAX_POOLSIZE);
					
					if (nodemaxpoolsize != null)
					{
						this.maxpoolsize = nodemaxpoolsize.getText();
					}
					Node nodecacheswitch = dsu.selectSingleNode(CACHE_SWITCH);
					
					if (nodecacheswitch != null)
					{
						this.cacheswitch = nodecacheswitch.getText();
					}
					Node nodecachetype = dsu.selectSingleNode(CACHE_TYPE);
					
					if (nodecachetype != null)
					{
						this.cachetype = nodecachetype.getText();
					}
					Node nodecacheip = dsu.selectSingleNode(CACHE_IP);
					
					if (nodecacheip != null)
					{
						this.cacheip = nodecacheip.getText();
					}
					Node nodecacheport = dsu.selectSingleNode(CACHE_PORT);
					
					if (nodecacheport != null)
					{
						this.cacheport = nodecacheport.getText();
					}
					Node nodecacheexpire = dsu.selectSingleNode(CACHE_EXPIRE);
					
					if (nodecacheexpire != null)
					{
						this.cacheexpire = nodecacheexpire.getText();
					}
					Node synchroswitch = dsu.selectSingleNode(SYSCHRO_SWITCH);
					this.synchroswitch = synchroswitch.getText();
					if (synchroswitch != null)
					{
						this.synchroswitch = synchroswitch.getText();
					}
					Node nodesynchroport = dsu.selectSingleNode(SYSCHRO_PORT);
					
					if (nodesynchroport != null)
					{
						this.synchroport = nodesynchroport.getText();
					}
					Node nodegatewayip = dsu.selectSingleNode(GATWAY_IP);
					
					if (nodegatewayip != null)
					{
						this.gatewayip = nodegatewayip.getText();
					}
					
					Node nodegatewayport = dsu.selectSingleNode(GATEWAY_PORT);
					
					if (nodegatewayport != null)
					{
						this.gatewayport = nodegatewayport.getText();
					}
					
					Node nodebaselength = dsu.selectSingleNode(BASE_LENGTH);
					
					if (nodebaselength != null)
					{
						this.baselength = nodebaselength.getText();
					}
					
					Node nodelogtype = dsu.selectSingleNode(LOG_TYPE);
					
					if (nodelogtype != null)
					{
						this.logtype = nodelogtype.getText();
					}
					
					Node nodeloglevel = dsu.selectSingleNode(LOG_LEVEL);
					if (nodeloglevel != null)
					{
						this.loglevel = Integer.parseInt(nodeloglevel.getText());
					}
					
					Node noderemoteloglevel = dsu.selectSingleNode(REMOTELOG_LEVEL);
					if (noderemoteloglevel != null)
					{
						this.remoteloglevel = Integer.parseInt(noderemoteloglevel.getText());
					}
					
					Node nodelogserverip = dsu.selectSingleNode(LOG_SERVERIP);
					if (nodelogserverip != null)
					{
						this.logserverip = nodelogserverip.getText();
					}
					
					Node nodeserviceport = dsu.selectSingleNode(SERVICE_PORT);
					if (nodeserviceport != null)
					{
						this.serviceport = nodeserviceport.getText();
					}
					Node nodeinvokeport = dsu.selectSingleNode(INVOKE_PORT);
					if (nodeinvokeport != null)
					{
						this.invokeport = nodeinvokeport.getText();
					}
				}
			}
			// 获取文件中的元素
			
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
	}
}
